﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NetDon.OA.Business;
using NetDon.OA.Interface.Admin;
using NetDon.OA.Interface.Configuration;
using NetDon.OA.Portal.Admin;
using NetDon.OA.Portal.Constants;
using NetDon.Utilities;
using NetDon.WebControls;
using NineRays.WebControls;

namespace NetDon.OA.Portal.Controls
{
    public partial class UserSelector1 : System.Web.UI.Page
    {
        public Guid Id
        {
            get { return ViewState["Id"] != null ? (Guid)ViewState["Id"] : Guid.Empty; }

            set { ViewState["Id"] = value; }
        }

        public Guid? ParentId
        {
            get { return ViewState["ParentID"] != null ? (Guid?)ViewState["ParentID"] : null; }

            set
            {
                ViewState["ParentID"] = value == Guid.Empty ? null : value;
            }
        }

        public NodeType CurrentNodeType
        {
            get { return ViewState["CurrentNodeType"] != null ? (NodeType)ViewState["CurrentNodeType"] : NodeType.Company; }

            set { ViewState["CurrentNodeType"] = value; }
        }
        public NodeType NewNodeType
        {
            get { return ViewState["NewNodeType"] != null ? (NodeType)ViewState["NewNodeType"] : NodeType.Root; }

            set { ViewState["NewNodeType"] = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
               
                BindCompany();
            }
        }
        #region Company

        private void BindCompany()
        {
            var service = ServiceContainer.GetService<ICompanyService>();
            var rootCategories = service.GetRootCompanies();
            treeCompany.Nodes.Clear();
            //add root company
            treeCompany.Nodes.Add(CreateRootNode());
            for (int i = 0; i < rootCategories.Count; i++)
            {
                treeCompany.Nodes[0].ChildNodes.Add(CreateCompanyTreeNode(rootCategories[i]));
            }

            treeCompany.Nodes[0].Text = Server.HtmlEncode(SystemConfiguration.RootCompanyName);
            treeCompany.Nodes[0].PopulateNodesOnDemand = rootCategories.Count > 0 ? true : false;
            treeCompany.SelectedNode = treeCompany.Nodes[0];
            Id = Guid.Empty;
            ParentId = null;
            CurrentNodeType = NodeType.Root;
            NewNodeType = NodeType.Root;
        }

        private FlyTreeNode CreateRootNode()
        {
            return new FlyTreeNode(
                            Server.HtmlEncode(SystemConfiguration.RootCompanyName),
                            Guid.Empty.ToString())
            {
                NodeTypeID = NodeType.Root.ToString(),
                ImageUrl = ApplicationImages.TreeNodeClosed,
                SelectedImageUrl = ApplicationImages.TreeNodeOpened,
                Expanded = true,
                ExpandLevel = 99,
                Tag = -1
            };
        }

        private FlyTreeNode CreateCompanyTreeNode(CompanyData companyData)
        {
            return new FlyTreeNode(
                            Server.HtmlEncode(companyData.CompanyName),
                            companyData.CompanyId.ToString())
            {
                NodeTypeID = NodeType.Company.ToString(),
                PopulateNodesOnDemand = companyData.HasChildren,
                ImageUrl = ApplicationImages.TreeNodeClosed,
                SelectedImageUrl = ApplicationImages.TreeNodeOpened,
                Tag = companyData.NodeLevel
            };
        }

        protected void treeCompany_PopulateNodes(object sender, FlyTreeNodeEventArgs e)
        {
            if (e.Node.ChildNodes.Count <= 0)
            {
                LoadChildNodes(e.Node);
            }
        }

        private void LoadChildNodes(FlyTreeNode node)
        {
            if (node.ChildNodes.Count <= 0)
            {
                //load companies
                var service = ServiceContainer.GetService<ICompanyService>();
                var childCompanies = (CompanyDataCollection)service.GetCompany(node.Value.ToGuid()).GetChildren();
                for (int i = 0; i < childCompanies.Count; i++)
                {
                    node.ChildNodes.Add(CreateCompanyTreeNode(childCompanies[i]));

                }

                ////load deparments
                var childDepartments = ServiceContainer.GetService<IDepartmentService>().GetRootDepartments(node.Value.ToGuid());
                for (int i = 0; i < childDepartments.Count; i++)
                {
                    node.ChildNodes.Add(CreateDepartmentTreeNode(childDepartments[i]));

                }
            }
        }
        private FlyTreeNode CreateDepartmentTreeNode(DepartmentData departmentData)
        {
            return new FlyTreeNode(
                            Server.HtmlEncode(departmentData.DepartmentName),
                            departmentData.DepartmentId.ToString())
            {
                NodeTypeID = NodeType.Department.ToString(),
                PopulateNodesOnDemand = departmentData.HasChildren,
                ImageUrl = ApplicationImages.DepartNodeClosed,
                SelectedImageUrl = ApplicationImages.DepartNodeOpened,
                Tag = departmentData.NodeLevel
            };
        }
        #endregion

        protected void btnDisplay_Click(object sender, EventArgs e)
        {
            gridUsers.Visible = true;
            PagerUsers.PerformQuery();
        }


        protected void gridUsers_RowDataBound(object sender, GridViewRowEventArgs e)
        {

        }

        protected void PagerUsers_PerformGetPageData(object sender, PerformGetPageDataEventArgs e)
        {

        }

        protected void PagerUsers_PerformGetRecordCount(object sender, PerformGetRecordCountEventArgs e)
        {

        }

        protected void PagerUsers_PerformGetCondition(object sender, PerformGetConditionEventArgs e)
        {

        }
    }
}